﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using NHibernate;
using WebSite.Core;
using WebSite.Data.NH.Infrastructure;
using WebSite.Model;

namespace WebSite.Data.NH.Repositories
{
    public interface IAdminUserRepository : IRepository<AdminUserInfo>
    {
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="passWord">密码</param>
        /// <returns>用户对象</returns>
        AdminUserInfo Login(string userName, string passWord);
        /// <summary>
        /// 密码修改
        /// </summary>
        /// <param name="adminInfo">用户信息</param>
        /// <returns>是否更新成功</returns>
        bool UpdatePassWord(AdminUserInfo adminInfo);
        /// <summary>
        /// 根据用户id获取用户对象
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        AdminUserInfo GetAdminInfo(int userId);
    }
    public class AdminUserRepository : NhRepositoryBase<AdminUserInfo>, IAdminUserRepository
    {
        /// <summary>
        /// DESCrypt 的摘要说明。
        /// snipershen
        /// MD5加密解密类
        /// xhshen@guanghua.sh.cn
        /// 2003/11/20
        /// </summary>
        public class MD5Crypt
        {
            public MD5Crypt()
            {
                //
                // TODO: 在此处添加构造函数逻辑
                //
            }

            public static string Encrypt(string strData)
            {
                byte[] md5Bytes = Encoding.Default.GetBytes(strData);

                // compute MD5 hash.
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] cryptString = md5.ComputeHash(md5Bytes);

                int iLen;
                string strTemp = String.Empty;

                iLen = cryptString.Length;

                for (int i = 0; i < iLen; i++)
                {
                    strTemp += cryptString[i].ToString("X2");
                }
                return strTemp;
            }
        }

        public AdminUserRepository(ISession session)
            : base(session)
        {

        }

        public AdminUserInfo Login(string userName, string passWord)
        {
            AdminUserInfo info = Get(m => m.UserName.Equals(userName) && m.Password.Equals(MD5Crypt.Encrypt(passWord)));
            if (info != null)
            {
                return info;
            }
            else
            {
                return null;
            }
        }


        public bool UpdatePassWord(AdminUserInfo adminInfo)
        {
            adminInfo.Password = MD5Crypt.Encrypt(adminInfo.Password);
            bool result = Update(adminInfo);
            return result;
        }


        public AdminUserInfo GetAdminInfo(int userId)
        {
            AdminUserInfo info = GetById(userId);
            return info;
        }
    }
}
